const CloudDBFunction = require('./cloudDbUtils')

let myHandler = async function (event, context, callback, logger) {
  const utils = new CloudDBFunction(logger)
  var _body,
    action,
    phone,
    uid,
    title,
    main_body,
    date,
    type,
    ip_address,
    email,
    name,
    introduce,
    career,
    id,
    portrait,
    article_id,
    search_content
  if (event.body) {
    _body = JSON.parse(event.body)
    action = _body.action
    phone = _body.phone
    uid = _body.uid
    title = _body.title
    main_body = _body.main_body
    date = _body.date
    type = _body.type
    ip_address = _body.ip_address
    email = _body.email
    name = _body.name
    introduce = _body.introduce
    career = _body.career
    id = _body.id
    article_id = _body.article_id
    portrait = _body.portrait
    search_content = _body.search_content
  } else {
    action = event.action
    phone = event.phone
    uid = event.uid
    title = event.title
    main_body = event.main_body
    date = event.date
    type = event.type
    ip_address = event.ip_address
    email = event.email
    name = event.name
    introduce = event.introduce
    career = event.career
    id = event.id
    article_id = event.article_id
    portrait = event.portrait
    search_content = event.search_content
  }
  switch (action) {
    case 'QUERY_USER':
      utils.queryUser(uid).then((user) => {
        callback({
          code: 200,
          data: user
        })
      })
      break
    case 'INSERT_USER':
      utils.insertUser(uid, name, phone, email, career).then(() => {
        callback({
          code: 201
        })
      })
      break
    case 'QUERY_INFORMATION':
      utils.queryInformation().then((information) => {
        callback({
          code: 202,
          data: information
        })
      })
      break
    case 'INSERT_ARTICLE':
      utils.insertArticle(uid, title, ip_address, date, main_body, type).then(() => {
        callback({
          code: 203
        })
      })
      break
    case 'QUERY_ARTICLE':
      utils.queryArticles(type).then((articles) => {
        callback({
          code: 204,
          data: articles
        })
      })
      break
    case 'UPDATE_UPVOTE':
      utils.updateUpvote(id, uid).then(() => {
        callback({
          code: 205
        })
      })
      break
    case 'UPDATE_RE_UPVOTE':
      utils.unUpdateUpvote(id, uid).then(() => {
        callback({
          code: 206
        })
      })
      break
    case 'UPDATE_READING':
      utils.updateReading(id, uid).then(() => {
        callback({
          code: 207
        })
      })
      break
    case 'INSERT_DISCUSS':
      utils.insertDiscuss(uid, id, date, name, main_body).then(() => {
        callback({
          code: 208
        })
      })
      break
    case 'QUERY_DISCUSS':
      utils.queryDiscuss(article_id).then((discuss) => {
        callback({
          code: 209,
          data: discuss
        })
      })
      break
    case 'UPDATE_PORTRAIT':
      utils.updatePortrait(uid, portrait).then((discuss) => {
        callback({
          code: 210,
          data: discuss
        })
      })
      break
    case 'QUERY_ARTICLE_BY_SEARCH_CONTENT':
      utils.queryArticlesBySearchContent(search_content).then((articles) => {
        callback({
          code: 211,
          data: articles
        })
      })
      break
    case 'QUERY_ARTICLE_BY_UID':
      utils.queryArticlesByUid(uid).then((articles) => {
        callback({
          code: 212,
          data: articles
        })
      })
      break
    case 'DELETE_ARTICLE':
      utils.deleteArticle(id).then(() => {
        callback({
          code: 213
        })
      })
      break
    case 'QUERY_NEWS':
      utils.queryNews().then((news) => {
        callback({
          code: 214,
          data: news
        })
      })
      break
    case 'UPDATE_NAME':
      utils.updateName(uid, name).then((news) => {
        callback({
          code: 214,
          data: news
        })
      })
      break
    case 'UPDATE_INTRODUCE':
      utils.updateIntroduce(uid, introduce).then((news) => {
        callback({
          code: 214,
          data: news
        })
      })
      break
    default:
      callback({
        code: 400,
        data: _body,
        desc: "choose error"
      })
      break
  }
}

export { myHandler }